import java.io.*; 
import java.math.BigDecimal;
import java.util.ArrayList;

public class SimulationSingleSource {

	Packet pcktHeader;
	ArrayList <Packet> allPackets;
	BigDecimal totalTime;
	long totalSize;
	float timePassed;
	long packetCounter;
	float masterClock;
	Packet pckt;
	ArrayList <Packet> queue;
	ArrayList delay;
	float prevPacketTime ;



	public SimulationSingleSource() {
		super();
		pcktHeader = new Packet();
		allPackets =  new ArrayList<Packet>();
		totalTime = new BigDecimal("0");
		totalSize =0;
		timePassed = 0;
		packetCounter = 0;
		masterClock = 0;
		pckt = new Packet();
		queue = new ArrayList<Packet>();
		delay = new ArrayList();
		prevPacketTime = 0;
	}



	public void readFile()
	{
		try{
			FileReader fr = new FileReader("out.txt"); 
			BufferedReader br = new BufferedReader(fr); 
			String s; 
			String lastLine = "";
			while((s = br.readLine()) != null) { 
				parseFile(s,false);
				lastLine =s;
				//System.out.println(s); 
			} 
			parseFile(lastLine,true);

			fr.close(); 
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void parseFile(String packetInfo, boolean endFile)
	{
		String [] packetHeader = packetInfo.split("\\s+");
		
		calculateInformation(packetHeader[0],packetHeader[1], packetHeader[3], endFile);
		//System.out.println(packetInfo.getArrivalTime());
	}

	public void calculateInformation(String packetNo, String packetSize, String time, boolean endFile)
	{
		
		pckt = new Packet();



		if(endFile)
		{
			System.out.println(packetCounter + "\t" + totalSize + "\t"+ timePassed +"\t" + time);

		}
		else
		{
			float newTime = timePassed+ Float.parseFloat(time);
			//	System.out.println(time);
			if(newTime >=1)
			{
				System.out.println(packetCounter +  "\t" + totalSize + "\t"+ timePassed +"\t" + prevPacketTime);
				totalTime = new BigDecimal("0");
				timePassed = 0;
				totalSize = 0;
				packetCounter = 0;
				processQueue();
			}
			else
			{
		    prevPacketTime = Float.parseFloat(time); 
			totalSize = totalSize + Long.parseLong(packetSize);
			BigDecimal tempTime = new BigDecimal(time);
			timePassed = timePassed+ Float.parseFloat(time);
			masterClock = masterClock + Float.parseFloat(time);
			packetCounter++;
			pckt.setArrivalTime(masterClock);
			pckt.setPacketNumber(packetNo);
			pckt.setTotalSize(Long.parseLong(packetSize));
			queue.add(pckt);
			//	System.out.println(packetNo + "\t" + totalSize + "\t" +timePassed);
			
			
		

			}

		}
	}
	public void processQueue()
	{
		long totalSize = 0;
		
	
			while (totalSize <= 3600000 && queue.size()> 0)
			{
				Packet thisPacket = queue.remove(0);
				//pcktCounter++;
				delay.add((masterClock - thisPacket.getArrivalTime()));
				totalSize = totalSize + thisPacket.totalSize;
			}
			
			//System.out.println("Total Size " + totalSize + " queue Size " + queue.size());
			
	
	}
	
	public void displayDelay()
	{
		System.out.println("Total packets in delay :" + delay.size() + " remaining packets in the queue " + queue.size());
		/*for (int i =0; i < delay.size(); i++)
		{
			System.out.println("Delay :" + (delay.get(i)));
		}*/
	}

	public void storePacket(Packet donePacket)
	{
		allPackets.add(donePacket);
	}

	public static void main (String args[])
	{
		SimulationSingleSource ss = new SimulationSingleSource();
		ss.readFile();
		ss.displayDelay();
	}





}
